<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Card\Controller;
use Think\Controller\RestController;

/**
 * 前台首页控制器
 * 主要获取首页聚合数据
 */
class MyController extends CardController {
    
    protected $login_session_id = 'DFDSFDFslfsfdlsdidsf';
    
    
   /*
   * sns登录
   *
   */
  public function  login(){
    if(IS_POST){
//      $phone=15918722145;
//      $password=123456789;
//      $cation=1;
        $phone=I('post.phone','');
        $password=I('post.password','');
        $phone1=I('post.phone1','');
        $phonecaptcha1=I('post.phonecaptcha1','');
        $cation=I('post.cation','','intval');
        if($cation==''){
            return $this->ajaxReturn(array('code'=>-1,'desc'=>"参数错误"));
        }

        $source=I('post.source',1,'intval');

        if($source==5){
             if (!$phone) {
                return $this->ajaxReturn(array('code'=>-1,'desc'=>"请输入邮箱"));
            }

            $error_key='邮箱';

        }else{
            if (!preg_match('/^1[3|4|5|7|8]\d{9}$/', $phone)) {
                return $this->ajaxReturn(array('code'=>-1,'desc'=>"手机号错误"));
            }
            $error_key='手机号';
        }

        
        
        if($cation==1){
            //密码登录
            if($phone=='' && $password==''){
                return $this->ajaxReturn(array('code'=>-1,'desc'=>$error_key."密码不能为空"));
            }
            $data['phone'] = $phone;
            $data['passWord'] = $password;          
            $data['source'] = $source;
            $url = C('DEV_WTTAI_API').'/member/login';

            $info  = getApiDataByUrl($url, $data);
            if($info['data']['memberId']==''){
                return $this->ajaxReturn(array('state'=>-1,'text'=>$error_key."或密码错误"));
            }else{
                $this->cardLogin($info['data']['memberId'],$info['data']);                
                if($info['data']['phone_bind']){
                    setcookie ("phone_bind", 1, time()+3600*24, '/' , 'wttai.com');
                    return $this->ajaxReturn(array('state'=>1,'text'=>"登录成功"));
                }else{
                    setcookie ("phone_bind", 0, time()+3600*24, '/' , 'wttai.com');
                    return $this->ajaxReturn(array('state'=>2,'text'=>"登录成功,请先绑定手机","url"=>C('SHEQU_DOMAIN').'/member/bind-phone.html?email=1'));
                }


                // dump($this->_member['memberId']);exit;
                //return $this->ajaxReturn(array('state'=>1,'text'=>"登录成功"));
            }
            
        }elseif($cation==2){
            //短信登录
            if($phone1=='' && $phonecaptcha1==''){
                return $this->ajaxReturn(array('state'=>-1,'text'=>$error_key."与验证码不能为空"));
            }
            $data['phone']=$phone1;
            $data['phoneCaptcha']=$phonecaptcha1;
            $data['source'] = '1';
           $post_data =array(
                 'phone' => $cosp['phone'],
                 'verify' => $applyForm_note
            );
           $member =curlShequData('member/autoreg', $post_data, 'POST');
           if($member['code']!="1"){
                $this->error($member['desc']);
           }            
            

            $info  = getApiDataByUrl($url, $data);
            if($info['data']['memberId']==''){
                return $this->ajaxReturn($info);
            }else{
                $this->snsLogin($info['data']['memberId'],$info['data']);
                return $this->ajaxReturn(array('state'=>1,'text'=>"登录成功"));
            }
            
        }else{
            $this->ajaxReturn(array('state'=>1,'text'=>'访问错误'));
        }
        
     }else{
         $this->ajaxReturn(array('state'=>1,'text'=>'访问方式错误'));
     }

  } 
    
    public function shequlogin()
    {
      $member = decrypt($_POST['sign'], 'wen');
      if ($member) {
          $member = unserialize($member);
      } else {
        exit(json_encode(array("code" => "0" )));
      }

      
      //获取用户信息
           $memberId = $member['memberId'];
           $url = C('DEV_WTTAI_API').'/member/info';
           $postData = array('memberId'=>$memberId);
           $info = getApiDataByUrl($url,$postData);

        $this->cardLogin($memberId,$info['data']);  
        exit(json_encode(array("code" => "1" )));

/*
exit(json_encode($member));
      var_dump($member);exit;
      $model_member   = Model('member');

      $member_id = $model_member->shequlogin($member);

      if ($member_id < 1) {
        exit(json_encode(array("code" => "0" )));
      }

      $login_info = array();



      $member_info = $model_member->member_id_login($member['memberId']);

      if(isset($member_info['error'])) {
          exit(json_encode(array("code" => "0" )));
      }

      $model_member->createSession($member_info, true);

      exit(json_encode(array("code" => "1" )));*/


    }

    /**
     * 登陆页面
     * 
     */
    public function login1() {
        
        // dump($this->login_session_id);exit;
       if (session($this->login_session_id)) {
           return redirect('/add.html');
       } 
       if (IS_POST) {
            $map = array();
            $map['moblie_phone'] = I('post.mobile', null, 'trim');
            if (preg_match('/^1[3|4|5|7|8]\d{9}$/', $map['moblie_phone'])) {
                $password = md5(I('post.password', null, 'trim'));
                $user = D('User')->field('id,password')->where($map)->find();
                if ($user['password'] == $password) {
                    session($this->login_session_id, $user['id']);
                    return $this->success('登陆成功', '/add.html');
                } else {
                    $this->assign('error', '密码错误');
                }
            } else {
                $this->assign('error', '手机号错误');
            }
        }   
        $this->display('login_1');
    }
    
    public function add() {
        $id = session($this->login_session_id);
        if ($id < 1) {
            return redirect('/login.html');
        }
        $user = D('User');
        if (IS_POST) {
            $data = array();
            $data['name'] = I('post.name', null, 'trim');
            $data['nickname'] = I('post.nickname', null, 'trim');
            $data['company'] = I('post.company', null, 'trim');
            $data['department'] = I('post.department', null, 'trim');
            $data['position'] = I('post.position', null, 'trim');
            $data['cover'] = I('post.cover', null, 'trim');
            $data['telephone'] = I('post.telephone', null, 'trim');
            $data['moblie_phone'] = I('post.mobliePhone', null, 'trim');
            $data['email'] = I('post.email', null, 'trim');
            $data['homepage_url'] = I('post.homepageUrl', null, 'trim');
            $data['weibo'] = I('post.weibo', null, 'trim');
            $data['qq'] = I('post.qq', null, 'trim');
            $data['wx_image'] = I('post.wxImage', null, 'trim');
            $data['weixin'] = I('post.weixin', null, 'trim');
            $data['address'] = I('post.address', null, 'trim');
            $data['intro'] = I('post.intro', null, 'trim');
            $files = $this->upload();
            if (is_array($files)) {
                if ($files['headImg'] && $files['headImg']['id'] > 0) {
                    $data['head_img'] = $files['headImg']['id'];
                }
                if ($files['wxImage'] && $files['wxImage']['id'] > 0) {
                    $data['wx_image'] = $files['wxImage']['id'];
                }
            } else {
                $error = $files;
            }
            if ($user->create($data, 2)) {
                $status = $user->where(array('id'=>$id))->save($data);
                if ($status || $status === 0) {
                    $this->assign('success', '编辑成功');
                } else {
                    $error = $user->getError();
                }
            } else {
                $error = '数据不能为空';
            }
            if ($error) {
                $this->assign('error', $error);
            }
        }  
        $info = $user->where(array('id' => $id))->find();
        $this->assign('info', $info);
        $this->assign('self_url', __SELF__);
        $this->display('add_1');
    }
    
    /**
     * 上传图片
     * 
     * @return type
     * 
     */
    protected function upload() {
        $error = 0;
        foreach ($_FILES as $file) {
            if ($file['error'] != 4) {
                $error = 1;
            }
        } 
        if ($error == 0) {
            return null;
        }
        $Picture = D('Picture');
        $pic_driver = C('PICTURE_UPLOAD_DRIVER');
        $info = $Picture->upload(
            $_FILES,
            C('PICTURE_UPLOAD'),
            C('PICTURE_UPLOAD_DRIVER'),
            C("UPLOAD_{$pic_driver}_CONFIG")
        ); //TODO:上传到远程服务器
        if($info){
            return $info;
        } else {
            return $Picture->getError();
        }
    }
    
    public function register() {
        if (IS_POST) {
            $data = array();
            $data['moblie_phone'] = I('post.mobile', null, 'trim');
            $data['password'] = I('post.password', null, 'trim');
            $user = D('User');
            $status = $user->add($data);
            if ($status) {
                session($this->login_session_id, $status);
                return $this->success('注册成功', '/add.html');
            } else {
                $error = $user->getError();
                $this->assign('error', $error);
            }
        }   
        $this->display('register_1');
    }
    
    
    
    
    
    
    
}